<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>js集合Map之基础示例</title>
</head>
<body>
  <form action="#">
    <label>接受协议：<input type="checkbox" name="accept" error="请接受协议才能提交"></label>
    <label>我是会员：<input type="checkbox" name="member" error="只对会员开放此功能"></label>
    <input type="button" value="提交">
  </form>
  <script>
    document.querySelector('input[type=button]').addEventListener('click', () => {
      const map = new Map()
      const inputs = document.querySelectorAll('input[name]')
      inputs.forEach(item => {
        map.set(item, {
          checked: item.checked,
          error: item.getAttribute('error')
        })
      })
      const result = [...map].every(([elem, config]) => {
        config.checked || alert(config.error)
        return config.checked
      })
      if (result) {
        alert('提交成功!')
      }
    })
  </script>
</body>
</html>